<?php

namespace app\modules\device\models;

use app\common\base\CommonModel;
use yii\helpers\ArrayHelper;

class Device extends CommonModel
{
    //数据库表名设置
    public static function tableName()
    {
        return '{{%device}}';
    }

    /**
     * rules
     *
     * @return array
     */
    public function rules()
    {
        return [
            [['deviceName', 'ownerId'], 'required'],
            [['deviceName'], 'string'],
            [['ownerId'], 'integer'],
            [['bind_timestamp', 'update_timestamp'], 'integer'],
        ];
    }

    public function behaviors()
    {
        return parent::behaviors();
    }

    /**
     * beforeSave
     *
     * @param bool $insert
     */
    public function beforeSave($insert)
    {
        if ($insert) {
            // $this->status = 1;
        }

        return parent::beforeSave($insert);
    }

    public function fields(){
        $fields = parent::fields();

        if(isset($fields['bind_timestamp'])){
            $fields['create_time'] = function($model){
                return date("Y-m-d H:i", $model->bind_timestamp/1000);
            };
        }
        if(isset($fields['update_timestamp'])){
            $fields['update_time'] = function($model){
                return date("Y-m-d H:i", $model->update_timestamp/1000);
            };
        }
        return $fields;
    }
}